package com.huichaoliu.stylist.activity;

import com.huichaoliu.app.R;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class StylistShoppingFragment extends Fragment implements
		OnClickListener {
	// tools
	Context context;
	FragmentManager fm;

	// views
	private View v = null;
	TextView view_all;
	TextView view_clothes;
	TextView view_pants;
	TextView view_shoes;

	// fragment
	ShoppingIncludeFragment fragmentAll;
	ShoppingIncludeFragment fragmentClothes;
	ShoppingIncludeFragment fragmentPants;
	ShoppingIncludeFragment fragmentShoes;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (v == null) {
			v = inflater.inflate(R.layout.fragment_stylist_shopping, container,
					false);
		}
		ViewGroup parent = (ViewGroup) v.getParent();
		if (parent != null) {
			parent.removeView(v);
		}
		context = getActivity();
		fm = ((FragmentActivity) context).getSupportFragmentManager();
		init();
		showAll();
		return v;
	}

	private void init() {
		view_all = (TextView) v.findViewById(R.id.shopping_all);
		view_clothes = (TextView) v.findViewById(R.id.shopping_clothes);
		view_pants = (TextView) v.findViewById(R.id.shopping_pants);
		view_shoes = (TextView) v.findViewById(R.id.shopping_shoes);

		view_all.setOnClickListener(this);
		view_clothes.setOnClickListener(this);
		view_pants.setOnClickListener(this);
		view_shoes.setOnClickListener(this);

		changePosition(0);
	}

	@SuppressWarnings("deprecation")
	private void changePosition(int code) {
		view_all.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.stylist_showpic_p));
		view_clothes.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.stylist_showpic_p));
		view_pants.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.stylist_showpic_p));
		view_shoes.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.stylist_showpic_p));

		switch (code) {
		case 0:
			view_all.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			break;
		case 1:
			view_clothes.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			break;
		case 2:
			view_pants.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			break;
		case 3:
			view_shoes.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			break;
		default:
			break;
		}
	}

	private void showAll() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentAll == null) {
			fragmentAll = new ShoppingIncludeFragment();
			fragmentAll.setTypePart(0);
		}

		if (fragmentAll.isAdded()) {
			ft.show(fragmentAll);
		} else {
			ft.add(R.id.shopping_fragment, fragmentAll);
			ft.show(fragmentAll);
		}

		ft.commit();

	}

	private void showClothes() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentClothes == null) {
			fragmentClothes = new ShoppingIncludeFragment();
			fragmentClothes.setTypePart(1);
		}

		if (fragmentClothes.isAdded()) {
			ft.show(fragmentClothes);
		} else {
			ft.add(R.id.shopping_fragment, fragmentClothes);
			ft.show(fragmentClothes);
		}

		ft.commit();
	}

	private void showPants() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentPants == null) {
			fragmentPants = new ShoppingIncludeFragment();
			fragmentPants.setTypePart(2);
		}

		if (fragmentPants.isAdded()) {
			ft.show(fragmentPants);
		} else {
			ft.add(R.id.shopping_fragment, fragmentPants);
			ft.show(fragmentPants);
		}

		ft.commit();
	}

	private void showShoes() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentShoes == null) {
			fragmentShoes = new ShoppingIncludeFragment();
			fragmentShoes.setTypePart(3);
		}

		if (fragmentShoes.isAdded()) {
			ft.show(fragmentShoes);
		} else {
			ft.add(R.id.shopping_fragment, fragmentShoes);
			ft.show(fragmentShoes);
		}

		ft.commit();
	}

	private void hideAll() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentAll != null && fragmentAll.isAdded()) {
			ft.hide(fragmentAll);
		}
		if (fragmentClothes != null && fragmentClothes.isAdded()) {
			ft.hide(fragmentClothes);
		}
		if (fragmentPants != null && fragmentPants.isAdded()) {
			ft.hide(fragmentPants);
		}
		if (fragmentShoes != null && fragmentShoes.isAdded()) {
			ft.hide(fragmentShoes);
		}
		ft.commit();

	}

	@Override
	public void onClick(View arg0) {
		hideAll();
		switch (arg0.getId()) {
		case R.id.shopping_all:
			changePosition(0);
			showAll();
			break;
		case R.id.shopping_clothes:
			changePosition(1);
			showClothes();
			break;
		case R.id.shopping_pants:
			changePosition(2);
			showPants();
			break;
		case R.id.shopping_shoes:
			changePosition(3);
			showShoes();
			break;

		default:
			break;
		}
	}

	public void dissmissAll() {
		if (fragmentAll != null) {
			fragmentAll.dissmissAll();
		}
		if (fragmentClothes != null) {
			fragmentClothes.dissmissAll();
		}
		if (fragmentPants != null) {
			fragmentPants.dissmissAll();
		}
		if (fragmentShoes != null) {
			fragmentShoes.dissmissAll();
		}
	}

}
